var order = ['red', 'yellow', 'blue', 'green', 'red']
var tuiwa = require("../../tuiwa.js");
Page({
  data: {
    moveEffect:false,
    scrollWidth: 0,
    scrollLeftWidth: 0,
    windowWidth: 0,
    composeFiveBlessingsList: [
      {
        count: 0,
        name: "友善福",
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-80751611631181362.png",
        noImgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-23651611631425050.png"
      },
      {
        count: 0,
        name: "和谐福",
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-5481611631367412.png",
        noImgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-23651611631425050.png"
      },
      {
        count: 0,
        name: "富强福",
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-85611611631407354.png",
        noImgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-23651611631425050.png"
      },
      {
        count: 0,
        name: "爱国福",
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-27801611631416457.png",
        noImgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-23651611631425050.png"
      },
      {
        count: 0,
        name: "敬业福",
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-97321611641193511.png",
        noImgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-23651611631425050.png"
      },
    ],

    hasCollectFiveBlessing: [],
    fiveBlessingsList: [
      {
        id: 0,
        num: 0,
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-21611641856747.png",
      },
      {
        id: 1,
        num: 0,
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-21471612266510327.png",
      },
      {
        id: 2,
        num: 0,
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-6391612266530718.png",
      },
      {
        id: 3,
        num: 0,
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-33501612266543141.png",
      },
      {
        id: 4,
        num: 0,
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-3571612266558728.png",
      },
      {
        id: 5,
        num: 0,
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-231612266576042.png",
      },
      {
        id: 6,
        num: 0,
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-70191612266603598.png"
      },
      {
        id: 7,
        num: 0,
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-34611612266616895.png"
      },
      {
        id: 8,
        num: 0,
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-54321611641992018.png"
      },
      {
        id: 9,
        num: 0,
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-72681611641999634.png"
      },
    ],
    blessingImgUrls: [
      {
        name: "友善福",
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-88291611646267084.png"
      },
      {
        name: "敬业福",
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-4651611646273891.png"
      },
      {
        name: "富强福",
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-4651611646273891.png"
      },
      {
        name: "和谐福",
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-49061611646287495.png"
      },
      {
        name: "爱国福",
        imgUrl: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-28211611646293325.png"
      },
    ],
    collectFiveBlessingsLayer: false,
    familyLuckInfo: false,

    autoplay: true,
    interval: 500,
    duration: 500,
    circular: true,
    vertical: true,

    top: 0,
    blessingResult: false,
    blessingImgUrl: '',
    blessingName: "",
    swiperIndex: 0,
    moveDistance: null,

    //抽取按钮次数
    requestCount: 0,
    comopseBaiDian: false,

    //当前合成的图片
    currentComposeImg: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-96171611641985287.png",
    composeShow: false,
    uncomposeShow: false,

    //抽取按钮图片
    chouquBtn: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-97241611632647667.png",
    kanshipinbtn: "https://fcdn.qiushilaile.com/adx/app-30521611751566010.png",
    bukanshipinbtn: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-97241611632647667.png",

    gototiefubtn: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-66941611642394555.png",
    cantototiefubtn: "https://fcdn.qiushilaile.com/adx/adx-46941611745014878.png",
    uncantotiefubtn: "https://fcdn.qiushilaile.com/adx/qualificationMaterials-66941611642394555.png",
    fiveBlessingComposed: false,

    testLoadingLayerInfo: true,
    testLoadingLayerBg: false,
    widthProgressInfo: 0,

    noFiveBlessing: false,
  },
  onLoad: function (options) {
    setTimeout(() => {
      this.setData({
        testLoadingLayerInfo: false
      })
    }, 4000)
    setTimeout(() => {
      this.setData({
        testLoadingLayerBg: true
      })
    }, 500)

    this.getScrollWidth();

    let composeFiveStorage = tt.getStorageSync('composeFiveBlessingsList');
    if (composeFiveStorage) {
      this.setData({
        composeFiveBlessingsList: composeFiveStorage
      })
    }

    //获取已经合成的全家福
    let fiveBlessingsStorage = tt.getStorageSync('hasCollectFiveBlessing');
    if (fiveBlessingsStorage) {
      this.setData({
        hasCollectFiveBlessing: fiveBlessingsStorage,
        gototiefubtn: "https://fcdn.qiushilaile.com/adx/adx-46941611745014878.png"
      })
    }
    console.log(fiveBlessingsStorage.length, "ddddddddddddddddd")
    if (fiveBlessingsStorage.length > 0) {
      this.setData({
        familyLuckInfo: true
      })
    }

    //重置时间 和可抽取次数

    let currentDate = new Date();
    let currentDay = currentDate.getDate()
    var oldDay = tt.getStorageSync("currentDay")
    if (currentDay != oldDay) {
      tt.setStorageSync('currentDay', currentDay);
      tt.setStorageSync('requestCount', 0);
      this.setData({
        requestCount: 0
      })
    } else {
      this.setData({
        requestCount: tt.getStorageSync('requestCount')
      })
    }

    if (this.data.requestCount >= 5) {
      this.setData({
        chouquBtn: this.data.kanshipinbtn
      })
    }

  },
  back: function () {
    tt.navigateBack();
  },
  getScrollWidth() {
    var scrollWidth = wx.getSystemInfoSync().windowWidth;
    console.log(scrollWidth / 3 * this.data.hasCollectFiveBlessing.length, "ssssss")

    this.setData({
      scrollWidth: scrollWidth / 3 * this.data.hasCollectFiveBlessing.length,
      windowWidth: scrollWidth
    })
  },
  scrollleft(e) {
    console.log(e);
  },
  lowerRight(e) {
    console.log(e);
  },

  unredirectToTieFu() {
    this.setData({
      uncomposeShow: false
    })
  },
  youshanani(clickTarget, currentTarget) {
    console.log(currentTarget.left, currentTarget.top)
    console.log(clickTarget.x, clickTarget.y)

    let animation = tt.createAnimation({
      duration: 3000,
      timingFunction: 'ease',
    })
    animation.translate((clickTarget.x - currentTarget.left), (clickTarget.y - currentTarget.top)).step()

    this.setData({
      youshanani: animation.export()
    })
  },
  hexieani(clickTarget, currentTarget) {
    let animation = tt.createAnimation({
      duration: 3000,
      timingFunction: 'ease',
    })

    animation.translate((clickTarget.x - currentTarget.left), (clickTarget.y - currentTarget.top)).step()

    this.setData({
      hexieani: animation.export()
    })
  },
  fuqiangani(clickTarget, currentTarget) {
    let animation = tt.createAnimation({
      duration: 3000,
      timingFunction: 'ease',
    })

    animation.translate((clickTarget.x - currentTarget.left), (clickTarget.y - currentTarget.top)).step()

    this.setData({
      fuqiangani: animation.export()
    })
  },
  aiguoani(clickTarget, currentTarget) {
    let animation = tt.createAnimation({
      duration: 3000,
      timingFunction: 'ease',
    })

    animation.translate((clickTarget.x - currentTarget.left), (clickTarget.y - currentTarget.top)).step()

    this.setData({
      aiguoani: animation.export()
    })
  },
  jingyeani(clickTarget, currentTarget) {
    let animation = tt.createAnimation({
      duration: 3000,
      timingFunction: 'ease',
    })

    animation.translate((clickTarget.x - currentTarget.left), (clickTarget.y - currentTarget.top)).step()

    this.setData({
      jingyeani: animation.export()
    })
  },

  composeClick(e) {
    let canFlag = true;
    this.data.composeFiveBlessingsList.forEach((item) => {
      console.log(item)
      if (item.count < 1) {
        canFlag = false;
      }
    })
    if (!canFlag) {
      this.setData({
        noFiveBlessing: true
      })

      setTimeout(() => {
        this.setData({
          noFiveBlessing: false
        })
      }, 2000)
      return;
    }

    this.data.composeFiveBlessingsList.forEach((item) => {
      item.count = item.count - 1
    })
    this.setData({
      composeFiveBlessingsList: this.data.composeFiveBlessingsList
    })
    tt.setStorageSync('composeFiveBlessingsList', this.data.composeFiveBlessingsList)


    // console.log(e,"eeeeeeeee")
    var that = this;
    console.log(e.detail.x, e.detail.y)
    let clickTarget = {
      x: e.detail.x - 20,
      y: e.detail.y - 20
    }
    this.setData({
      comopseBaiDian: true
    })
    // setTimeout(() => {
    //   const query = tt.createSelectorQuery();
    //   // 选择所有类名为 flex-item 的节点，并添加一个对它们布局信息的查询
    //   query.selectAll(".composeImage").boundingClientRect();
    //   // 执行查询
    //   query.exec((res) => {
    //     console.log(res, "rrrrrrrrrrrrr");
    //     that.youshanani(clickTarget, res[0][0])
    //     that.hexieani(clickTarget, res[0][1])
    //     that.fuqiangani(clickTarget, res[0][2])
    //     that.aiguoani(clickTarget, res[0][3])
    //     that.jingyeani(clickTarget, res[0][4])
    //   });
    // }, 1000)

    let currentComposeImg = this.data.fiveBlessingsList[this.data.hasCollectFiveBlessing.length].imgUrl

    this.data.hasCollectFiveBlessing.push(this.data.fiveBlessingsList[this.data.hasCollectFiveBlessing.length])
    tt.setStorageSync('hasCollectFiveBlessing', this.data.hasCollectFiveBlessing);

    //获取已经合成的全家福
    let fiveBlessingsStorage = tt.getStorageSync('hasCollectFiveBlessing');
    if (fiveBlessingsStorage) {
      this.setData({
        hasCollectFiveBlessing: fiveBlessingsStorage,
      })
    }
    console.log(fiveBlessingsStorage)
    if (fiveBlessingsStorage.length > 0) {
      this.setData({
        familyLuckInfo: true
      })
    }
    this.setData({
      gototiefubtn: "https://fcdn.qiushilaile.com/adx/adx-46941611745014878.png"
    })
    this.setData({
      moveEffect:true
    })
    setTimeout(() => {
      this.setData({
        currentComposeImg: currentComposeImg,
        composeShow: true,
        comopseBaiDian: false,
        fiveBlessingComposed: true,
        moveEffect:false
      })
    }, 3300)

    setTimeout(() => {
      this.setData({
        fiveBlessingComposed: false
      })
    }, 4000)

    this.getScrollWidth();
  },
  closeAdFunction: function () {
    this.setData({
      collectFiveBlessingsLayer: true,
      top: 0,
      blessingResult: false,
      blessingImgUrl: '',
      blessingName: "",
      hasWatchVideo: true,
    })

  },
  cancelAdFunction: function () {
    console.log("没看完广告");
    tt.showModal({
      title: "温馨提示",
      content: "要观看完整视频才能进入哦~",
      showCancel: !1
    })
  },
  extractClick() {
    if (this.data.requestCount >= 5) {
      tt.showModal({
        title: '温馨提示',
        content: '点击确定前往观看视频',
        cancelText: '取消',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定');
            tuiwa.playAd();
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        }
      });
    } else {
      this.setData({
        collectFiveBlessingsLayer: true,
        top: 0,
        blessingResult: false,
        blessingImgUrl: '',
        blessingName: "",
      })
    }
  },
  redirectToTieFu() {

    this.setData({
      composeShow: false
    })
    tt.navigateTo({
      url: '../../pages/pasteBlessings/pasteBlessings' // 指定页面的url
    });
  },
  attachClick() {
    if (this.data.hasCollectFiveBlessing.length <= 0) {
      this.setData({
        uncomposeShow: true
      })
      return;
    }
    tt.navigateTo({
      url: `/pages/pasteBlessings/pasteBlessings`
    });
  },
  noResponse() {

  },
  viewTouchMove(e) {
    console.log("滑动的值是：", e.touches[0].clientY)
    if (e.touches[0].clientY <= 350 || e.touches[0].clientY >= 518) {
      return
    }
    this.setData({
      top: e.touches[0].clientY - 350,
      interval: 0,
      moveDistance: e.touches[0].clientY
    })
  },
  endMove(e) {
    this.setData({
      interval: 0,
    })
    if (this.data.moveDistance < 350) {
      return
    }
    if (this.data.moveDistance >= 500) {
      let getIndex = 0;
      var imgUrls = this.data.blessingImgUrls
      var hasGetFuIndex = tt.getStorageSync("hasGetFuIndex")
      if (!hasGetFuIndex) {
        hasGetFuIndex = []
        getIndex = Math.floor(Math.random() * 4)
        console.log(getIndex, "suijishu3")
      } else {
        console.log(Math.floor(Math.random() * 4), "suijishu")
        //如果看视频了。给一个没有的。
        // if(this.data.hasWatchVideo){

        // }else{

        // }
        if (hasGetFuIndex.length <= 4) {
          if (hasGetFuIndex.indexOf(0) < 0) {
            getIndex = 0;
          }
          if (hasGetFuIndex.indexOf(1) < 0) {
            getIndex = 1;
          }
          if (hasGetFuIndex.indexOf(2) < 0) {
            getIndex = 2;
          }
          if (hasGetFuIndex.indexOf(3) < 0) {
            getIndex = 3;
          }
        } else {
          if ( hasGetFuIndex.length == 5) {
            getIndex = 4;
          } else {
            getIndex = Math.floor(Math.random() * 5)
          }

        }

      }

      imgUrls.forEach((item, index) => {
        console.log(index, "iiiiiiiii")
        if (index == getIndex) {
          this.setData({
            blessingImgUrl: item.imgUrl,
            blessingName: item.name
          })


          //相应的福字数量加1；
          hasGetFuIndex.push(index)

          tt.setStorageSync('hasGetFuIndex', hasGetFuIndex);
          this.data.composeFiveBlessingsList.forEach((item, index) => {
            if (this.data.blessingName == item.name) {
              item.count = item.count + 1;
            }
          })
          this.setData({
            composeFiveBlessingsList: this.data.composeFiveBlessingsList,
            requestCount: Number(this.data.requestCount) + 1
          })

          tt.setStorageSync('composeFiveBlessingsList', this.data.composeFiveBlessingsList);
          tt.setStorageSync('requestCount', Number(this.data.requestCount));

          if (this.data.requestCount >= 5) {
            this.setData({
              chouquBtn: this.data.kanshipinbtn
            })
          }
        }
      })

      setTimeout(() => {
        this.setData({
          blessingResult: true
        })
      }, 1000)
      setTimeout(() => {
        this.setData({
          collectFiveBlessingsLayer: false
        })
      }, 3000)

    } else {
      tt.showToast({
        title: '请滑到最底部哦'
      });
    }

  },
  animationFinished(e) {
    this.setData({
      swiperIndex: e.detail.current
    })
  },
  onReady: function () {

  },
  onShow: function () {
    tuiwa.globalData.currentPage = this
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function (options) {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

